﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.Extensions;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.Articles
{
    public partial class Save : ManageBasePage
    {
        private IArticleCategoryTask _articleCatTask;
        private IArticleTask _articleTask;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("SaveArticle");
            _articleCatTask = GetInstance<IArticleCategoryTask>();
            _articleTask = GetInstance<IArticleTask>();

            if (!IsPostBack)
            {
                ddlCats.DataSource = _articleCatTask.GetAll().ToSelectList(c => c.Id.ToString(), c => c.CategoryName);
                ddlCats.DataBind();

                var id = 0;
                if (Request.QueryString["Id"] != null && Int32.TryParse(Request.QueryString["Id"], out id))
                {
                    var article = _articleTask.Get(id);
                    if (article == null)
                    {
                        ShowMsgAndReturn("参数错误");
                        return;
                    }

                    txtTitle.Text = article.Title;
                    txtContent.Value = article.Content;
                    chkIsShow.Checked = article.IsShow;
                    txtSortOrder.Text = article.SortOrder.ToString();
                    ddlCats.SelectedIndex =
                        ddlCats.Items.IndexOf(ddlCats.Items.FindByValue(article.ArticleCategory.Id.ToString()));
                    lblId.Text = article.Id.ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var content = txtContent.Value;
            if (string.IsNullOrEmpty(txtContent.Value.Trim()))
            {
                ShowMsg("请输入文章内容");
                return;
            }

            var title = txtTitle.Text.Trim();
            int sortOrder = 0;

            if (string.IsNullOrEmpty(content)
                || string.IsNullOrEmpty(title)
                || string.IsNullOrEmpty(txtSortOrder.Text.Trim())
                || !Int32.TryParse(txtSortOrder.Text.Trim(), out sortOrder))
            {
                ShowMsg("输入不完整");
                return;
            }

            var cmd = new ArticleCommand
                          {
                              ArticleCategoryId = Convert.ToInt32(ddlCats.SelectedValue),
                              ArticleId = string.IsNullOrEmpty(lblId.Text) ? (int?)null : Convert.ToInt32(lblId.Text),
                              Content = content,
                              Title = title,
                              IsShow = chkIsShow.Checked,
                              SortOrder = sortOrder
                          };

            using (var work = UnitOfWork.Begin())
            {
                if (_articleTask.Save(cmd) != null)
                {
                    work.Commit();
                    ShowMsg("保存文章成功", "Index.aspx");
                }
                else
                {
                    ShowMsg("保存文章失败");
                }
            }

        }
    }
}